package site.thatime.hyts.web.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import site.thatime.hyts.model.PdaProduct;
import site.thatime.hyts.model.Result;
import site.thatime.hyts.service.PdaProductService;

@RestController
@RequestMapping("/product")
public class ProductController {
	@Resource
	private PdaProductService pdaProductService;

	@RequestMapping(path = "/{id}", method = RequestMethod.GET)
	public PdaProduct fetch(@PathVariable("id") String id) {
		return pdaProductService.fetch(id);
	}

	@RequestMapping(path = "/all", method = RequestMethod.GET)
	public List<PdaProduct> getAll() {
		return pdaProductService.getAll();
	}

	@RequestMapping(path = "/download", method = RequestMethod.GET)
	public Result download() throws IOException {
		return new Result(true).setData(pdaProductService.getAll());
	}

//	@RequestMapping(path = "/download", method = RequestMethod.GET)
//	public ResponseEntity<byte[]> download() throws IOException {
////		return new Result(true).setData(pdaProductService.getAll());
//		final Charset utf8 = CharsetUtil.CHARSET_UTF_8;
//		HttpHeaders headers = new HttpHeaders();// http头信息
//		String downloadFileName = new String("product.json".getBytes(utf8), CharsetUtil.CHARSET_ISO_8859_1);// 设置编码
//		headers.setContentDispositionFormData("attachment", downloadFileName);
//		// MediaType:互联网媒介类型 contentType：具体请求中的媒体类型信息
//		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//
//		ByteArrayOutputStream out = new ByteArrayOutputStream(512);
//
////		out.write("[".getBytes(utf8));
//		List<PdaProduct> products = pdaProductService.getAll();
//		boolean first = true;
//		for (PdaProduct p : products) {
//			out.write(JSONObject.toJSONBytes(p, SerializerFeature.QuoteFieldNames));
//			if (first) {
//				first = false;
//			}
//			out.write("\r\n".getBytes(utf8));
//			out.flush();
//		}
////		out.write("]".getBytes(utf8));
////		out.flush();
//		return new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.CREATED);
//	}
}
